package Tests
{
	import Box2D.Collision.Shapes.b2CircleShape;
	import Box2D.Collision.Shapes.b2EdgeShape;
	import Box2D.Collision.Shapes.b2PolygonShape;
	import Box2D.Common.Math.b2Vec2;
	import Box2D.Common.Math.b2Vec2Array;
	import Box2D.Dynamics.Joints.b2JointDef;
	import Box2D.Dynamics.Joints.b2WeldJointDef;
	import Box2D.Dynamics.b2Body;
	import Box2D.Dynamics.b2BodyDef;
	import Box2D.Dynamics.b2BodyType;
	import Box2D.Dynamics.b2FixtureDef;
	
	import Framework.Test;
	
	import org.osmf.layout.AbsoluteLayoutFacet;
	
	public class Cantilever extends Test
	{
		private static const e_count:int = 8;
		
		public function Cantilever()
		{
			super();
			createGround();
			createJoint1();
			createJoint2();
			createJoint3();
			createJoint4();
			createBodies();
		}
		private function createGround():void{
			var shape:b2EdgeShape = new b2EdgeShape;
			shape.Set(new b2Vec2(-40,0),new b2Vec2(40,0));
			m_groundBody.CreateFixtureByShape(shape,0);
		}
		private function createJoint1():void{
			var shape:b2PolygonShape = new b2PolygonShape;
			shape.SetAsBox(0.5, 0.125);
			
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = shape;
			fd.density = 20.0;
			
			var jd:b2WeldJointDef = new b2WeldJointDef;
			
			var prevBody:b2Body = m_groundBody;
			for (var i:int = 0; i < e_count; ++i)
			{
				var bd:b2BodyDef = new b2BodyDef;
				bd.type = b2BodyType.b2_dynamicBody;
				bd.position.Set(-14.5 + 1.0 * i, 5.0);
				var body:b2Body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
				
				var anchor:b2Vec2 = new b2Vec2(-15.0 + 1.0 * i, 5.0);
				jd.Initialize(prevBody, body, anchor);
				m_world.CreateJoint(jd);
				
				prevBody = body;
			}			
		}
		private function createJoint2():void{
			var shape:b2PolygonShape = new b2PolygonShape;
			shape.SetAsBox(0.5, 0.125);
			
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = shape;
			fd.density = 20.0;
			
			var jd:b2WeldJointDef = new b2WeldJointDef;
			
			var prevBody:b2Body = m_groundBody
			for (var i:int = 0; i < e_count; ++i)
			{
				var bd:b2BodyDef = new b2BodyDef;
				bd.type = b2BodyType.b2_dynamicBody;
				bd.position.Set(-14.5 + 1.0 * i, 15.0);
				var body:b2Body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
				
				var anchor:b2Vec2 = new b2Vec2(-15.0 + 1.0 * i, 15.0);
				jd.Initialize(prevBody, body, anchor);
				m_world.CreateJoint(jd);
				
				prevBody = body;
			}			
		}
		private function createJoint3():void{
			var shape:b2PolygonShape = new b2PolygonShape;
			shape.SetAsBox(0.5, 0.125);
			
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = shape;
			fd.density = 20.0;
			
			var jd:b2WeldJointDef = new b2WeldJointDef;
			
			var prevBody:b2Body = m_groundBody
			for (var i:int = 0; i < e_count; ++i)
			{
				var bd:b2BodyDef = new b2BodyDef;
				bd.type = b2BodyType.b2_dynamicBody;
				bd.position.Set(-4.5 + 1.0 * i, 5.0);
				var body:b2Body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
				
				if (i > 0)
				{
					var anchor:b2Vec2 = new b2Vec2(-5.0 + 1.0 * i, 5.0);
					jd.Initialize(prevBody, body, anchor);
					m_world.CreateJoint(jd);
				}
				
				prevBody = body;
			}			
		}
		private function createJoint4():void{
			var shape:b2PolygonShape = new b2PolygonShape;
			shape.SetAsBox(0.5, 0.125);
			
			var fd:b2FixtureDef = new b2FixtureDef;
			fd.shape = shape;
			fd.density = 20.0;
			
			var jd:b2WeldJointDef = new b2WeldJointDef;
			
			var prevBody:b2Body = m_groundBody
			for (var i:int = 0; i < e_count; ++i)
			{
				var bd:b2BodyDef = new b2BodyDef;
				bd.type = b2BodyType.b2_dynamicBody;
				bd.position.Set(5.5 + 1.0 * i, 10.0);
				var body:b2Body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
				
				if (i > 0)
				{
					var anchor:b2Vec2 = new b2Vec2(5.0 + 1.0 * i, 10.0);
					jd.Initialize(prevBody, body, anchor);
					m_world.CreateJoint(jd);
				}
				
				prevBody = body;
			}			
		}
		private function createBodies():void{
			var i:int;
			var bd:b2BodyDef = new b2BodyDef;
			var fd:b2FixtureDef = new b2FixtureDef;
			var body:b2Body;
			for (i = 0; i < 2; ++i)
			{
				var vertices:b2Vec2Array = new b2Vec2Array(3);
				vertices.setAt2(0,-0.5, 0.0);
				vertices.setAt2(1,0.5, 0.0);
				vertices.setAt2(2,0, 1.5);
				
				var shape:b2PolygonShape = new b2PolygonShape;
				shape.Set(vertices,3);
				
				fd.shape = shape;
				fd.density = 1.0;
				
				bd.type = b2BodyType.b2_dynamicBody;
				bd.position.Set(-8.0 + 8.0 * i, 12.0);
				body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
			}
			
			for (i = 0; i < 2; ++i)
			{
				var circle:b2CircleShape = new b2CircleShape
				circle.radius = 0.5;
				
				fd.shape = circle;
				fd.density = 1.0;
				
				bd.type = b2BodyType.b2_dynamicBody;
				bd.position.Set(-6.0 + 6.0 * i, 10.0);
				body = m_world.CreateBody(bd);
				body.CreateFixture(fd);
			}			
		}
	}
}